package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallStoreTagMapper;
import org.linlinjava.litemall.db.dao.TagMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:	标签管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-11 9:29:44
 */
@Service
@Transactional
public class LitemallTagService {
    @Autowired
    private LitemallStoreTagMapper storeTagMapper;
    @Autowired
    private TagMapper tagMapper;

    public LitemallStoreTag findById(Integer id) {
        return storeTagMapper.selectByPrimaryKey(id);
    }


    public void add(LitemallStoreTag entity) {
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        storeTagMapper.insertSelective(entity);
    }

    public int updateById(LitemallStoreTag entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return storeTagMapper.updateByPrimaryKeySelective(entity);
    }

    public List<TagVo> querySelective(String name, Integer page, Integer size, String sort, String order) {


        PageHelper.startPage(page, size);
        return tagMapper.selectByExample(name);
    }

    public int count() {
        LitemallStoreTagExample example = new LitemallStoreTagExample();
        example.or().andDeletedEqualTo(false);

        return (int) storeTagMapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        storeTagMapper.logicalDeleteByPrimaryKey(id);
    }

    public List<LitemallStoreTag> findTag(String name) {
        LitemallStoreTagExample example = new LitemallStoreTagExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return storeTagMapper.selectByExample(example);
    }


    public Object[] findListByStoreId(Integer storeId) {
        Object[] collect = tagMapper.selectByStoreId(storeId).stream().map(tagVo -> tagVo.getId()).collect(Collectors.toList()).toArray();
        return collect;
    }

    public List<LitemallStoreTag> findListByCategoryId(Integer id) {
        LitemallStoreTagExample example = new LitemallStoreTagExample();
        example.or().andTagCategoryIdEqualTo(id).andDeletedEqualTo(false);
        return storeTagMapper.selectByExampleSelective(example,new LitemallStoreTag.Column[]{LitemallStoreTag.Column.id, LitemallStoreTag.Column.name,LitemallStoreTag.Column.picUrl, LitemallStoreTag.Column.iconUrl});
    }
}
